package com.huitone.gddw.service.impl.worktable;

import com.huitone.gddw.dao.entity.PriAlloc;
import com.huitone.gddw.dao.entity.SvrNodeCodeRlt;
import com.huitone.gddw.dao.entity.SvrNodeDef;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISvrNodeDefDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IPriAllocService;
import com.huitone.gddw.service.ISvrNodeCodeRltService;
import com.huitone.gddw.service.ISvrNodeDefService;
import com.huitone.gddw.service.dto.SvrNodeDefDTO;
import com.huitone.gddw.utils.Base64Utils;
import com.huitone.gddw.utils.SysLoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Title SvrNodeDefService
* @Package com.huitone.gddw.service.impl
* @Description TODO
* @author chengsiyue
* @date 2017年4月26日 下午6:04:40
* @version V1.0   
*/
@Service
public class SvrNodeDefService implements ISvrNodeDefService {
	@Autowired
	private ISvrNodeDefDao SvrNodeDefDao;
	@Autowired
	private ISvrNodeCodeRltService svrNodeCodeRltService;
	@Autowired
	private IPriAllocService priAllocService;
	@Autowired
	private SvrNodeCodeRlt svrNodeCodeRlt;

	@Override
	public int deleteByPrimaryKey(String svrNode) {
		svrNodeCodeRltService.deleteBySvrNode(svrNode);
		return SvrNodeDefDao.deleteByPrimaryKey(svrNode);
	}

	@Override
	public int save(SvrNodeDef record) {
		record.setSvrNode("2"+SvrNodeDefDao.selectForSvrNode());
		record.setInsertStaffId(SysLoginUtil.getLoginUserId());
		record.setInsertTime(new Date());
		this.encodePwdBase64(record);
		return SvrNodeDefDao.save(record);
	}

	@Override
	public int insert(SvrNodeDef record) {
		return 0;
	}

	@Override
	public SvrNodeDef selectByPrimaryKey(String svrNode) {
		SvrNodeDef entity = SvrNodeDefDao.selectByPrimaryKey(svrNode);
		this.decodePwdBase64(entity);
		return entity;
	}

	@Override
	public int update(SvrNodeDef record) {
		record.setLastModStaffId(SysLoginUtil.getLoginUserId());
		record.setLastModTime(new Date());
		this.encodePwdBase64(record);
		return SvrNodeDefDao.update(record);
	}

	@Override
	public int updateByPrimaryKey(SvrNodeDef record) {
		record.setLastModStaffId(SysLoginUtil.getLoginUserId());
		record.setLastModTime(new Date());
		this.encodePwdBase64(record);
		return SvrNodeDefDao.updateByPrimaryKey(record);
	}

	@Override
	public QueryPage<SvrNodeDefDTO> selectPage(QueryPage<SvrNodeDef> qp) {
		Map<String,Object> pramas=qp.getParams();
		pramas.put("start", (qp.getPageNo() - 1) * qp.getPageSize()+1);
		pramas.put("end", (qp.getPageNo() - 1) * qp.getPageSize() + qp.getPageSize());
		List<SvrNodeDefDTO> result = SvrNodeDefDao.selectPage(pramas);
		
		for (SvrNodeDefDTO svrNodeDefDTO : result) {
			this.decodePwdBase64(svrNodeDefDTO);
		}
		
		int totalCount=SvrNodeDefDao.selectCount(pramas);
		QueryPage<SvrNodeDefDTO> queryPage = new QueryPage<>();
		queryPage.setResult(result);
		queryPage.setPageNo(qp.getPageNo());
		queryPage.setPageSize(qp.getPageSize());
		queryPage.setTotalCount(totalCount);

		return queryPage;
	}

	@Override
	public List<SvrNodeDefDTO> getRecord(Map<String,Object> map) {
		List<SvrNodeDefDTO> result = SvrNodeDefDao.getRecord(map);

		for (SvrNodeDefDTO svrNodeDefDTO : result) {
			this.decodePwdBase64(svrNodeDefDTO);
		}
		
		return result;
	}

	@Override
	public SvrNodeDefDTO selectForUpdate(String svrNode) {
		SvrNodeDefDTO dto = new SvrNodeDefDTO( SvrNodeDefDao.selectForUpdate(svrNode));
		this.decodePwdBase64(dto);
		return dto;
	}

	@Override
	public void updateServiceCode(String svrNode, List<String> saveList, List<String> delList) {
		
		for (String serviceCode : saveList) {
//			serviceCodeDefService.save(serviceCode);
			
			svrNodeCodeRlt.setSvrNode(svrNode);
			svrNodeCodeRlt.setServiceCode(serviceCode);
			svrNodeCodeRlt.setInsertStaffId(SysLoginUtil.getLoginUserId());
			svrNodeCodeRlt.setInsertTime(new Date());
			
			svrNodeCodeRltService.save(svrNodeCodeRlt);
			
			//插入优先级分配记录 
			PriAlloc entity=new PriAlloc();
			entity.setServiceCode(serviceCode);
			entity.setSvrNode(svrNode);
			entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
			entity.setInsertTime(new Date());
			priAllocService.save(entity);
		}
		for (String serviceCode : delList) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("svrNode", svrNode);
			map.put("serviceCode", serviceCode);
			
//			serviceCodeDefService.delete(serviceCode);
			svrNodeCodeRltService.deleteByServiceCode(map);
			priAllocService.deleteByCode(svrNode, serviceCode);
		}
	}

	@Override
	public List<Map<String, Object>> getSvrNode() {
		List<Map<String, Object>> list = SvrNodeDefDao.getAllSvrNodeName();
		return list;
	}

	/**
	 * 获取本单位业务端信息
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getUnitSvrNode(String userId) {
		List<Map<String, Object>> list = SvrNodeDefDao.getFinalSvrNodeName(userId);
		return list;
	}

	/**
	 * 加密
	 * @param entity SvrNodeDef
	 */
	private void encodePwdBase64(SvrNodeDef entity) {
		entity.setLoginPwd(Base64Utils.encode(entity.getLoginPwd()));
		entity.setLoginSvrPwd(Base64Utils.encode(entity.getLoginSvrPwd()));
	}
	
	/**
	 * 解密
	 * @param entity SvrNodeDef
	 */
	private void decodePwdBase64(SvrNodeDef entity) {
		entity.setLoginPwd(Base64Utils.decode(entity.getLoginPwd()));
		entity.setLoginSvrPwd(Base64Utils.decode(entity.getLoginSvrPwd()));
	}
	
	/**
	 * 解密
	 * @param dto SvrNodeDefDTO
	 */
	private void decodePwdBase64(SvrNodeDefDTO dto) {
		dto.setLoginPwd(Base64Utils.decode(dto.getLoginPwd()));
		dto.setLoginSvrPwd(Base64Utils.decode(dto.getLoginSvrPwd()));
	}

	@Override
	public List<SvrNodeDef> selectBySvrNodeName(String svrNode, String svrNodeName) {
		return SvrNodeDefDao.selectBySvrNodeName(svrNode, svrNodeName);
	}

}
